#include "LED.h"
#include "Timer.h"

/* CONSTRUCTOR */
LED::LED(int LEDPort){
  m_LEDPort = LEDPort;
  pinMode(m_LEDPort, OUTPUT);
  m_timeEventId = -1;
}

/* METHODES */

/**
* \brief Verify if switch position has changed and update its state
* \param eventId
* \return none
*/
void LED::timerCallbackFunction(int eventId)
{
  if (eventId == m_timeEventId)
  {
    Serial.print(m_LEDState);
    Serial.print(" ");
    Serial.print(m_LEDPort);
    Serial.println();
    m_LEDState = !m_LEDState;
    digitalWrite(m_LEDPort, m_LEDState);
  }
  else
  {
    /* Wrong event */
  }
}

/**
* \brief Turn on the LED
* \param none
* \return none
*/
void LED::turnON()
{
  if (m_timeEventId != -1)
  {
    Timer::getTimer()->stop(m_timeEventId);
    m_timeEventId = -1;
  }
  else
  {
    /* Nothing to do, no event registered */
  }
  m_LEDState = HIGH;
  digitalWrite(m_LEDPort, m_LEDState);
}

/**
* \brief Turn off the LED
* \param none
* \return none
*/
void LED::turnOFF()
{
  m_LEDState = LOW;
  digitalWrite(m_LEDPort, m_LEDState);
}

/**
* \brief Makes the LED blink
* \param timeON, timeOFF, nbOfCycles
* \return none
*/
void LED::blink(int timeON, int nbOfCycles) 
{
  Timer* t = Timer::getTimer();
  m_timeEventId = t->every( timeON, this, nbOfCycles);
}

